package scau.erps.view.decision.card;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JOptionPane;

import scau.erps.app.Erps;
import scau.erps.entity.Loan;
import scau.erps.service.LoanService;
import scau.erps.service.impl.LoanServiceImpl;
import scau.erps.view.decision.finance.ShortLoanFinancePanel;
import scau.erps.view.decision.sand.HighLoanPanel;
import scau.erps.view.decision.sand.ShortLoanPanel;
import scau.erps.view.decision.table.LoanListPanel;

/**
 * 短贷决策
 * 
 * @author DCJ
 *
 */
public class ShortLoanCardPanel extends CardPanelTemplate {
	private static final long serialVersionUID = 1L;
	private ShortLoanPanel shortLoanPanel;
	private HighLoanPanel highLoanPanel;
	private ShortLoanFinancePanel shortLoanFinancePanel;
	private LoanListPanel loanListPanel;

	public ShortLoanCardPanel() {
		super();
		this.btn[4].setVisible(true);
		this.btn[5].setVisible(true);
		this.btn[6].setVisible(true);
		this.sandPanel.setLayout(new BorderLayout());
		shortLoanFinancePanel = new ShortLoanFinancePanel();
		shortLoanPanel = new ShortLoanPanel();
		highLoanPanel = new HighLoanPanel();
		loanListPanel = new LoanListPanel();
		this.sandPanel.add(shortLoanFinancePanel, BorderLayout.NORTH);
		this.sandPanel.add(shortLoanPanel, BorderLayout.WEST);
		this.sandPanel.add(highLoanPanel, BorderLayout.CENTER);
		this.sandPanel.add(loanListPanel, BorderLayout.EAST);

	}

	public void refresh() {
		this.shortLoanPanel.setTextFieldValue();
		this.highLoanPanel.setTextFieldValue();
		this.shortLoanFinancePanel.setTextFieldValue();
		this.loanListPanel.loadList();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 更新短贷
		if (e.getSource() == btn[4]) {
			JOptionPane.showMessageDialog(this, "系统自动更新，请核对操作盘面", "提示", JOptionPane.INFORMATION_MESSAGE);
			this.btn[4].setEnabled(false);
			this.btn[5].setEnabled(true);
		}
		// 偿还短贷
		else if (e.getSource() == btn[5]) {
			LoanService loanService = Erps.getIocInstance().get(LoanServiceImpl.class);
			List<Loan> shortLoans = loanService.getReturnLoans(Erps.getCurrentTeam().getId(), "短贷");
			List<Loan> highLoans = loanService.getReturnLoans(Erps.getCurrentTeam().getId(), "高利贷");
			int n = shortLoans.size()+highLoans.size();
			int amount = 0;
			int interest = 0;
			for (Loan loan : shortLoans) {
				amount = amount + loan.getAmount();
				interest = interest + loan.getInterest();
			}
			for (Loan loan : highLoans) {
				amount = amount + loan.getAmount();
				interest = interest + loan.getInterest();
			}
			int cash = Erps.getSand().getCash();				
			if(cash>=(amount+interest)){
				loanService.repayShortLoan(shortLoans);
				loanService.repayShortLoan(highLoans);
				String message = n+"笔短贷款合计"+amount+"M已成功还款,利息为"+interest+ "M,请更新盘面";
				JOptionPane.showMessageDialog(null, message, "提醒", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep();
				this.refresh();
			}else{
				JOptionPane.showMessageDialog(null, "当前现金不足偿还短贷、高利贷以及利息，请先融资！", "警告", JOptionPane.WARNING_MESSAGE);
			}

		}
		// 申请贷款
		else if (e.getSource() == btn[6]) {
			LoanService loanService = Erps.getIocInstance().get(LoanServiceImpl.class);
			String[] amount = { "20", "40", "60" };
			// 加入规则，小于所有者权益2倍
			String s = (String) JOptionPane.showInputDialog(null, "请选择贷款额度:\n", "申请短贷", JOptionPane.PLAIN_MESSAGE, null, amount, "20");
			if (s != null) {
				String info = "申请贷款" + s + "M成功，请前往银行领取现金！";
				loanService.applyShortLoan(Integer.parseInt(s));
				JOptionPane.showMessageDialog(null, info, "提醒", JOptionPane.INFORMATION_MESSAGE);
			}
			this.changeStep(); // 切换步骤
			this.refresh();
		}
	}

	@Override
	public String setRule() {
		String rule = "(1)短贷利息为5%;\r\n(2)现有贷款加上申请贷款数量小于上年所有者权益2倍";
		return rule;
	}
}
